﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class dauNoiTbSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public dauNoiTbSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public List<DAUNOI_THIETBI> getlistBySiteid(int id)
        {
            try
            {
                return dbContext.DAUNOI_THIETBI.Where(p => p.SITEID == id).ToList();
            }
            catch (Exception e)
            {
                List<DAUNOI_THIETBI> _list = new List<DAUNOI_THIETBI>();
                return _list;
            }
        }

        public List<DAUNOI_THIETBI> getlist()
        {
            try
            {
                return dbContext.DAUNOI_THIETBI.ToList();
            }
            catch (Exception e)
            {
                List<DAUNOI_THIETBI> _list = new List<DAUNOI_THIETBI>();
                return _list;
            }
        }

        public DAUNOI_THIETBI getByid(long Id)
        {
            try
            {
                return dbContext.DAUNOI_THIETBI.Where(prop => prop.THIETBIID == Id).First();
            }
            catch (Exception e)
            {
                DAUNOI_THIETBI _list = new DAUNOI_THIETBI();
                return _list;
            }
        }

        public List<_listJsontoTree_DNTB> getListTreeAllDauNoiTbParent(string[] toQl, string[] donviVT, int? dauVao, int Stt)
        {
            List<_listJsontoTree_DNTB> _listreturn = new List<_listJsontoTree_DNTB>();

            var _listMVT = dbContext.DAUNOI_THIETBI.Where(p => p.MANGVT == "PON" && int.Equals(p.DAUVAO, dauVao) && toQl.Contains(p.TOQL) && donviVT.Contains(p.DONVIQL)).OrderBy(p => p.PORT_SO).ToList();
            foreach (var item in _listMVT)
            {
                _listJsontoTree_DNTB itemdauVaoFirst = new _listJsontoTree_DNTB();
                itemdauVaoFirst.id = item.THIETBIID.ToString(); //item.MA;
                itemdauVaoFirst.text = item.TEN;
                itemdauVaoFirst.type = Stt.ToString();
                itemdauVaoFirst.ThietbiId = item.THIETBIID;
                try
                {
                    itemdauVaoFirst.vntpSite = dbContext.VNPT_SITE.Where(p => p.SITEID == item.SITEID).First().TEN;
                }
                catch (Exception ex) { itemdauVaoFirst.vntpSite = ""; }
                itemdauVaoFirst.children = getListTreeAllDauNoiTb(itemdauVaoFirst.children, toQl, donviVT, item.THIETBIID, Stt += 1);
                _listreturn.Add(itemdauVaoFirst);
            }

            return _listreturn;
        }

        public List<_listJsontoTree_DNTB> getListTreeAllDauNoiTb(List<_listJsontoTree_DNTB> _listreturn, string[] toQl, string[] donviVT, int? dauVao, int Stt)
        {
            try
            {

                var _listMVT = dbContext.DAUNOI_THIETBI.Where(p => p.MANGVT == "PON" && p.DAUVAO.Value == dauVao && toQl.Contains(p.TOQL) && donviVT.Contains(p.DONVIQL)).OrderBy(p => p.PORT_SO).ToList();

                
                foreach (var item in _listMVT)
                {
                    _listJsontoTree_DNTB itemdauVaoFirst = new _listJsontoTree_DNTB();
                    itemdauVaoFirst.id = item.THIETBIID.ToString(); //item.MA;
                    itemdauVaoFirst.text = item.TEN;
                    itemdauVaoFirst.type = Stt.ToString();
                    itemdauVaoFirst.ThietbiId = item.THIETBIID;
                    try
                    {
                        itemdauVaoFirst.vntpSite = dbContext.VNPT_SITE.Where(p => p.SITEID == item.SITEID).First().TEN;
                    }
                    catch (Exception ex) { itemdauVaoFirst.vntpSite = ""; }
                    itemdauVaoFirst.children = getListTreeAllDauNoiTb(itemdauVaoFirst.children, toQl, donviVT, item.THIETBIID, Stt += 1);
                    _listreturn.Add(itemdauVaoFirst);
                }

                return _listreturn;

            }
            catch (Exception e)
            {
                List<_listJsontoTree_DNTB> _listRe = new List<_listJsontoTree_DNTB>();
                return _listRe;
            }
        }

        public List<DAUNOI_THIETBI> list_OLT_TOQL_DONVI(string[] toQl, string[] donviVT, string mangVt)
        {
            try
            {
                var _listMVT = dbContext.DAUNOI_THIETBI.Where(p => p.MANGVT == mangVt && int.Equals(p.DAUVAO, null) && toQl.Contains(p.TOQL) && donviVT.Contains(p.DONVIQL)).ToList();
                return _listMVT;
            }
            catch (Exception ex)
            {
                List<DAUNOI_THIETBI> _return = new List<DAUNOI_THIETBI>();
                return _return;
            }

        }

        public List<_listJsontoTree_DNTB> getListTreeAllDauNoiTb_Home_Page(string[] toQl, string[] donviVT, string mangVt)
        {
            try
            {
                List<_listJsontoTree_DNTB> _listAdd = new List<_listJsontoTree_DNTB>();
                var _listMVT = dbContext.DAUNOI_THIETBI.Where(p => p.MANGVT == mangVt && int.Equals(p.DAUVAO, null) && toQl.Contains(p.TOQL) && donviVT.Contains(p.DONVIQL)).ToList();
                foreach (var item in _listMVT)
                {
                    _listJsontoTree_DNTB itemdauVaoFirst = new _listJsontoTree_DNTB();
                    itemdauVaoFirst.id = item.THIETBIID.ToString(); //item.MA;
                    itemdauVaoFirst.text = item.TEN;
                    itemdauVaoFirst.type = "" ;
                    itemdauVaoFirst.ThietbiId = item.THIETBIID;
                    try
                    {
                        itemdauVaoFirst.vntpSite = dbContext.VNPT_SITE.Where(p => p.SITEID == item.SITEID).First().TEN;
                    }
                    catch (Exception ex) { itemdauVaoFirst.vntpSite = ""; }

                    _listAdd.Add(itemdauVaoFirst);

                }

                return _listAdd;
            }
            catch (Exception ex)
            {
                List<_listJsontoTree_DNTB> _return = new List<_listJsontoTree_DNTB>();
                return _return;
            }

        }

    }
}
